package com.lkx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkx.common.BaseContext;
import com.lkx.common.R;
import com.lkx.mapper.ShoppingCartMapper;
import com.lkx.pojo.ShoppingCart;
import com.lkx.service.ShoppingCartService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @ClassName: com.lkx.service.impl.ShoppingCartServiceImpl
 * @author: likaixin
 * @create: 2022年11月26日 18:04
 * @desc:
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    @Override
    public R<Object> sub(ShoppingCart shoppingCart) {
        LambdaUpdateWrapper<ShoppingCart> updateWrapper = new LambdaUpdateWrapper<>();
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper();
        if (shoppingCart.getDishId()!=null){//说明是菜品

            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
            ShoppingCart list = super.getOne(lqw);
            int number=0;
            if (list!=null){
                number = list.getNumber();
            }


            lambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());

            updateWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());

            updateWrapper.set(ShoppingCart::getNumber,number-1);
            super.update(updateWrapper);
            if (number==1){
                super.remove(lambdaQueryWrapper);

                return R.success(0);
            }

            return R.success(this.getOne(lqw));
        }else{//说明是套餐
           // LambdaUpdateWrapper<ShoppingCart> updateWrapper = new LambdaUpdateWrapper<>();
            //LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            ShoppingCart list = super.getOne(lqw);
            int number=0;
            if (list!=null){
                number = list.getNumber();
            }

            //LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper();
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

            updateWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

            updateWrapper.set(ShoppingCart::getNumber,number-1);

            super.update(updateWrapper);

            if (number==1){
                super.remove(lambdaQueryWrapper);
                return R.success(0);
            }

            return R.success(this.getOne(lqw));
        }

    }

    @Override
    public R<ShoppingCart> add(ShoppingCart shoppingCart) {
        //log.info("购物车数据：{}",shoppingCart);
        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //查询当前菜品或者套餐是否在购物车当中
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,currentId);
        if (dishId!=null){
            //说明添加到购物车的是菜品
            lqw.eq(ShoppingCart::getDishId,dishId);
        }else{
            //说明添加到购物车的是套餐
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //如果已存在，就在原来数量加1
        ShoppingCart shoppingCartServiceOne = super.getOne(lqw);
        if (shoppingCartServiceOne!=null){

            Integer number = shoppingCartServiceOne.getNumber();
            shoppingCartServiceOne.setNumber(number+1);
            super.updateById(shoppingCartServiceOne);

        }else{
            //如果不存在。则添加到购物车，数量默认是1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            super.save(shoppingCart);
            shoppingCartServiceOne=shoppingCart;
        }


        return R.success(shoppingCartServiceOne);
    }
}
